# Maintainer: Jo Zzsi <jozzsicsataban@gmail.com>
pkgname=dracut
pkgver=105
pkgrel=2
pkgdesc="An event driven initramfs infrastructure"
url="https://github.com/dracut-ng/dracut/wiki"
arch="noarch !riscv64 !armhf" # checkdepends fails
license="GPL-2.0-or-later"
makedepends="asciidoc musl-fts-dev kmod-dev bash coreutils blkid findmnt eudev"
checkdepends="linux-lts"
subpackages="$pkgname-modules-network $pkgname-tests $pkgname-modules $pkgname-dev $pkgname-doc $pkgname-bash-completion $pkgname-core::all"
triggers="$pkgname.trigger=/usr/share/kernel/*"
source="$pkgname-$pkgver.tar.gz::https://github.com/dracut-ng/dracut/archive/refs/tags/$pkgver.tar.gz
	README.alpine
	10-alpine.conf
	"
provides="initramfs-generator"
provider_priority=100 # low, somewhat experimental
builddir="$srcdir/$pkgname-ng-$pkgver"

build() {
	./configure --sysconfdir="/etc"
	CFLAGS="$CFLAGS -D__GLIBC_PREREQ=" CWD="$(pwd)" make
}

package() {
	depends="$pkgname-core $pkgname-modules-network"

	DESTDIR="$pkgdir" make install
	rm -rf "$pkgdir"/usr/lib/dracut/test/container "$pkgdir"/usr/lib/dracut/test/TEST*SYSTEMD*

	install -Dm644 "$srcdir"/README.alpine -t "$pkgdir"/usr/share/doc/$pkgname/
	install -Dm644 "$srcdir"/10-alpine.conf -t "$pkgdir"/usr/lib/dracut/dracut.conf.d/
}

network() {
	pkgdesc="network dracut modules"
	depends="dracut-modules dhclient iputils"

	for f in \
		cifs \
		fcoe \
		fcoe-uefi \
		iscsi \
		kernel-network-modules \
		livenet \
		nbd \
		net-lib \
		network \
		network-legacy \
		nfs \
		qemu-net \
		ssh-client \
		url-lib; do
		amove usr/lib/dracut/modules.d/[0-9][0-9]$f
	done

	if [ "$CARCH" = "s390x" ]; then
		amove usr/lib/dracut/modules.d/[0-9][0-9]zfcp
		amove usr/lib/dracut/modules.d/[0-9][0-9]znet
	fi
}

modules() {
	pkgdesc="local dracut modules"
	depends="dracut-core eudev util-linux-misc sed losetup"

	rm -rf "$pkgdir"/usr/lib/dracut/modules.d/*systemd*
	rm -rf "$pkgdir"/usr/share/man/man8/*.service.*
	rm -rf "$pkgdir"/usr/lib/kernel

	# systemd or distro dependent additional dracut modules
	for f in \
		bluetooth \
		connman \
		dbus* \
		fido2 \
		lvmmerge \
		memstrack \
		network-manager \
		pcsc \
		pkcs11 \
		rngd \
		squash* \
		tpm2-tss; do
		rm -rf "$pkgdir"/usr/lib/dracut/modules.d/[0-9][0-9]$f
	done

	if [ "$CARCH" != "s390x" ]; then
		for f in \
			cms \
			cio_ignore \
			cms \
			dasd* \
			dcssblk \
			zfcp \
			zipl \
			znet; do
			rm -rf "$pkgdir"/usr/lib/dracut/modules.d/[0-9][0-9]$f
		done
	else
		rm -rf "$pkgdir"/usr/lib/dracut/modules.d/[0-9][0-9]warpclock
	fi

	amove usr/lib/dracut/modules.d
}

core() {
	pkgdesc="core tools for dracut"
	depends="bash coreutils blkid findmnt grep"

	amove etc usr
}

tests() {
	pkgdesc="dracut tests"
	depends="dracut-modules e2fsprogs make qemu-img qemu-system-x86_64 sfdisk sudo"

	amove usr/lib/dracut/test usr/lib/dracut/dracut.conf.d/test*
	amove usr/lib/dracut/modules.d/[0-9][0-9]test*
}

check() {
	TESTS="98" KVERSION="lts" make check
}

sha512sums="
1608fb31d6a53905ea25a279586573db5fc7e084b4f6ff06e52065cbcb4ff503c2d51c0a282345844228232b1b590382b482a224183e0c4ee16c9c9e6932b275  dracut-105.tar.gz
fa1d65d8987d9b5846f5cd7989bc373ca4f9d787a3025284ce55e2d5439dce29f70bd75ff5e07bfa783548e080859ec8972b6887ec58993bc54a8dcc849066d6  README.alpine
f8afaedcce7f67600a3386fd95928a8706b59a62e69dc3da2351eb6d74e6c24c44f4a2c50117538fd9f19a356d4ecfc27b3581cb3a37fd6e094c52eba55ae0ed  10-alpine.conf
"
